﻿#include "errmessage.h"
#include "ui_errmessage.h"
extern QString allerror;
intListener infochan;
errmessage::errmessage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::errmessage)
{
    setWindowIcon(QIcon(":/pic/picture/96favicon.ico"));
    ui->setupUi(this);

    commonClient=CommonHelper::commonInstance();
    logClient=loghelper::getInstance();
    QObject::connect(&infochan, &intListener::valueChanged, this,&errmessage::valchange);
    ui->label_3->setWordWrap(true);
    ui->label_3->setText("通信故障");
    ui->label_6->setText("请确认设备通信是否正常连接，请点击返回，重新建立连接。");

}

errmessage::~errmessage()
{
    delete ui;
}

void errmessage::valchange(){
    ui->label_3->clear();
    ui->label_3->setText(allerror);
}

void errmessage::showEvent(QShowEvent *event)
{
    tcpClient=TcpClient::getInstance();

    if(MainWindow::tcpStatus.get()==1){
        ui->BeginBtn->setEnabled(false);
    }else if(MainWindow::tcpStatus.get()==0){
        ui->BeginBtn->setEnabled(true);
    }
}

void errmessage::on_returnBtn_clicked()
{
    this->close();
    emit changeMainWindow();
}


void errmessage::on_SaveBtn_clicked()
{
    this->close();
    emit changeMainWindow();
}


void errmessage::on_BeginBtn_clicked()
{
    // 创建一个question弹出对话框，添加两个按钮：Yes和No
    QMessageBox *box = new QMessageBox(QMessageBox::Question, "停止实验", "是否确认停止实验", QMessageBox::Yes | QMessageBox::No, this);
    box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
    box->button(QMessageBox::No)->setText("取消");// 将原本显示“No”的按钮改为显示“取消”
    int res = box->exec();// 启动对话框，用res变量记录用户最终点选的按钮

    if (res == QMessageBox::Yes) {

        QString data="00 00 00 01 00 04";
        QString finalData=QString("%1 %2 55 AA").arg(data,commonClient->GetCrcCheck(data.toLatin1()));
        QString dataLength=commonClient->TenToSixteen(finalData.remove(QRegExp("\\s")).length()/2,4);
        QString head= QString("62 00 00 3A %1 ").arg(dataLength);
        QString result=head+finalData;
        QByteArray sendInfo=commonClient->GetSendData(result);
        tcpClient->ClientSendingData(sendInfo);
        MainWindow::experimentStatus.set(2);
        this->close();
        emit changeMainWindow();
        logClient->WriteLog("CommLog","发送:\n"+result);
    }
    this->close();
    emit changeMainWindow();
}

